package
{
	import com.greensock.TweenLite;
	import com.greensock.easing.Back;
	import com.greensock.easing.Bounce;
	import com.greensock.easing.Elastic;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.filters.GlowFilter;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.ui.Mouse;
	import flash.utils.Dictionary;
	import flash.utils.Timer;
	import flash.utils.getTimer;

	public class Hud
	{
		public static const PADDING:int = 10;
		
		private var m_player:Player;
		public var display:Sprite = new Sprite();
		public var debugInfo:TextField = new TextField();
		
		private var updateTimer:Timer = new Timer(1000,0);
		private var tickTimer:Timer = new Timer(1000/60,0);
		
		public var m_lastTotalCash:int;
		public var m_lastPlayerCash:Number;
		public var dirty:Boolean = false;
		
		public var addStoreButton:Sprite;
		public var addShopperButton:Sprite;
		
		public var totalDeltaCash:int;
		public var m_lastUpdateTick:int;
		public var cashPerSecond:Number;
		public var m_startTime:int;
		
		private var buttonsArea:Sprite;
		private var topRightAnchor:Sprite;
		
		public var fwb:FWBSlot;
		
		public function Hud()
		{
			m_startTime = getTimer();

			updateTimer.addEventListener(TimerEvent.TIMER, onUpdate);
			display.addEventListener(Event.ENTER_FRAME, onEnterFrame);
			updateTimer.start();
			
			topRightAnchor = new Sprite();
			display.addChild(debugInfo);
			addStoreButton = Util.getButton('buy store',0xffffff,.5);
			addShopperButton = Util.getButton('advertise',0xffffff,.5);
			addShopperButton.x = addStoreButton.width + PADDING;
//			topRightAnchor.addChild(addStoreButton);
//			topRightAnchor.addChild(addShopperButton);
			 
			
			var fwbSim:FWBSimulator = new FWBSimulator();
			topRightAnchor.addChild(fwbSim.display);
			//fwb.display.addEventListener(MouseEvent.CLICK, onFWBClick);
			
			display.addChild(topRightAnchor);
			buttonsArea = new Sprite();
			
			var button:Sprite;
			
			button = Util.makeButton('assets/buttons/shopping_cart.png');
			buttonsArea.addChild(button);
			
			button = Util.makeButton('assets/buttons/shopping_cart.png');
			button.x = button.width+2;
			buttonsArea.addChild(button);

			button = Util.makeButton('assets/buttons/shopping_cart.png');
			button.y = button.height+2;
			buttonsArea.addChild(button);
			
			button = Util.makeButton('assets/buttons/shopping_cart.png');
			button.x = button.width+2;
			button.y = button.height+2;
			
			buttonsArea.addChild(button);			
			
			
			
//			display.addChild(buttonsArea);
			
			
			
			var lookup:Dictionary = new Dictionary(true);
			var friendBar:Sprite = new Sprite();
			// Friends pics to botom bar
			var snUsers:Vector.<SNUser> = getSNUsers();
			var i:int, length:int;
			for(i = 0, length = snUsers.length; i < length; i++){
				var pic:DisplayObject = ImageManager.getImage(snUsers[i].img);
				var snUserSlot:Sprite = Util.getRoundRectSprite(pic.width + PADDING, pic.height*2 + PADDING, Math.random()*0x777777 + 0x777777, .8, 10);
				
				lookup[snUserSlot] = snUsers[i];
				snUserSlot.addEventListener(MouseEvent.CLICK, function(me:MouseEvent):void{ });
				snUserSlot.addChild(pic);
				pic.x = snUserSlot.width/2 - pic.width/2;
				pic.y = PADDING/2;
				friendBar.addChild(snUserSlot);
				snUserSlot.x = PADDING*i + i*snUserSlot.width;
			}
			
			bottomBar = Util.getRoundRectSprite(friendBar.width + PADDING, friendBar.height + PADDING, 0, .7, 10);
			bottomBar.addChild(friendBar);
//			display.addChild(bottomBar);
			
			friendBar.x = bottomBar.width/2 - friendBar.width/2;
			friendBar.y = PADDING/2;
			
			
			
			doneButton = Util.getButton('Done!',0xff0000,.8);
			doneButton.scaleX = 0;
			doneButton.scaleY = 0;
			doneButton.visible = false;
			display.addChild(doneButton);
			
			onResize(new Event(''));
			Game.stage.addEventListener(Event.RESIZE, onResize);
		}
		public var score:int = 0;
		private function onFWBClick(me:MouseEvent):void{
			fwb.udpateScore(score += 1000);
			fwb.updateMessageField(String(score));
			fwb.applyBonusItem(ImageManager.getImage('assets/coins.png'));
		}
		
		public function init():void{
			debugInfo.x = display.parent.width - debugInfo.width;
			
			
			
		}
		
		private var coinTf:TextField;
		
		private function getSNUsers():Vector.<SNUser>{
			var snUsers:Vector.<SNUser> = new Vector.<SNUser>
			snUsers.push(new SNUser('Vinh', 'assets/profile_pics/vinh_t.jpg'));
			snUsers.push(new SNUser('Brian', 'assets/profile_pics/brian_r.jpg'));
			snUsers.push(new SNUser('Helen', 'assets/profile_pics/helen_z.jpg'));
			snUsers.push(new SNUser('Jesse', 'assets/profile_pics/jesse_smith.jpg'));
			snUsers.push(new SNUser('Mark', 'assets/profile_pics/mark_n.jpg'));
			snUsers.push(new SNUser('Paul', 'assets/profile_pics/paul_s.jpg'));
			
			return snUsers;
		}
		
		public var doneButton:Sprite;
		private var bottomBar:Sprite;
		private function updateBottomBar():void{
			bottomBar.y = Game.stage.stageHeight - bottomBar.height - PADDING;
			bottomBar.x = Game.stage.stageWidth/2 - bottomBar.width/2;
		}
		
		public function hideDoneButton():void{
			var finalPoint:Point = new Point(doneButton.x + doneButton.width/2, doneButton.y + doneButton.height/2);
			TweenLite.to(doneButton, .25, {scaleX: 0, scaleY: 0, x: finalPoint.x, y: finalPoint.y,
				onComplete: function():void{
					doneButton.visible = false;
				}
			});
		}
		
		public function showDoneButton():void{
			doneButton.visible = true;
			doneButton.scaleX = 1;
			doneButton.scaleY = 1;
			var finalPoint:Point = new Point(doneButton.x - doneButton.width/2, doneButton.y - doneButton.height/2);
			doneButton.scaleX = 0;
			doneButton.scaleY = 0;
			TweenLite.to(doneButton, .25, {scaleX: 1, scaleY: 1, x: finalPoint.x, y: finalPoint.y});
		}
		
		private function onResize(e:Event):void{
			buttonsArea.x = -buttonsArea.width + Game.stage.stageWidth - PADDING;
			buttonsArea.y = -buttonsArea.height + Game.stage.stageHeight - PADDING;
			
			doneButton.x = Game.stage.stageWidth/2 - doneButton.width/2;
			doneButton.y = 100;
			
			topRightAnchor.x = Game.stage.stageWidth - topRightAnchor.width;
			
			updateBottomBar();
		}
		private var updatePlayerCash:Boolean = false;
		private function onEnterFrame(e:Event):void{

		}
		
		private function onUpdate(te:TimerEvent):void{

		}
	}
}